package com.shunbo.convenient.controller;

import com.shunbo.convenient.service.GroupsService;
import com.shunbo.convenient.service.ManagerService;
import com.shunbo.convenient.utils.ConvenientResult;
import com.shunbo.convenient.utils.JsonUtils;
import com.shunbo.convenient.vo.Groups;
import com.shunbo.convenient.vo.Manager;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author weizhuang
 * 地址
 */
@RestController
@RequestMapping("/groups")
public class GroupsController {


    @Resource
    private GroupsService groupsService;
    @Resource
    private ManagerService managerService;


    /**
     * 组列表
     *
     * @param groups 组
     * @return Result结果集
     */
    @RequestMapping("/list")
    public ConvenientResult getGroupsList(@RequestBody Groups groups) {
        try {
            List<Groups> all = groupsService.findAll(groups);
            for (Groups group: all
                 ) {
                Manager manager = new Manager();
                manager.setGroupId(group.getId()+"");
                List<Manager> all1 = managerService.findAll(manager);
                group.setManagerList(all1);
            }
            return ConvenientResult.build(200, "成功", all);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }


    /**
     * 添加组
     *
     * @param groups 组
     * @return Result结果集
     */
    @RequestMapping("/save")
    public ConvenientResult saveGroups(@RequestBody Groups groups) {
        try {
            Integer save = groupsService.save(groups);
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 更新组
     *
     * @param groups 组
     * @return Result结果集
     */
    @RequestMapping("/update")
    public ConvenientResult updateGroups(@RequestBody Groups groups) {
        try {
            Integer save = groupsService.update(groups);
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 删除组
     *
     * @param groups 组
     * @return Result结果集
     */
    @RequestMapping("/delete")
    public ConvenientResult deleteGroups(@RequestBody Groups groups) {
        try {
            Integer save = groupsService.delete(groups.getId() + "");
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * id查组
     *
     * @param groups 组
     * @return Result结果集
     */
    @RequestMapping("/findOne")
    public ConvenientResult getGroupsById(@RequestBody Groups groups) {
        try {
            Groups st = groupsService.findById(groups.getId() + "");
            return ConvenientResult.build(200, "成功", st);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }



}
